﻿#region Usings

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Web;
using System.Web.Caching;
using Castle.MonoRail.Framework;
using OAuth.Net.Common;
using Rodunu.Movies.Contracts;
using Rodunu.Movies.Interfaces.Activity;
using Rodunu.Movies.Interfaces.Movie;
using Rodunu.Movies.Interfaces.Services;
using Rodunu.Movies.Interfaces.Subscriptions;
using Rodunu.Movies.Interfaces.User;
using Rodunu.Movies.Logic.Activity;
using Rodunu.Movies.Logic.Movie;
using Rodunu.Movies.Logic.Subscriptions;
using Rodunu.Movies.Logic.User;
using System.Linq;
using Rodunu.Movies.Services.NetFlix.DataProvider.Logic;
using Rodunu.Movies.Web.Utility;

#endregion

namespace Rodunu.Movies.Web.Controllers
{
    public class UserController : UserBaseController
    {
        #region Private Members

        private readonly IUser _userLogic;
        private readonly IShareMovies _shareMoviesLogic;
        private readonly IServices _servicesLogic;
        private readonly ISubscriptions _subscriptionsLogic;

        #endregion

        #region Constructor

        public UserController()
        {
            _userLogic = new UserLogic();
            _shareMoviesLogic = new ShareMoviesLogic();
            _servicesLogic = new ServicesLogic();
            _subscriptionsLogic = new SubscriptionsLogic();
        }

        #endregion

        #region All Users

        public void Users()
        {
            SetUser();
            SetLayout(true);

            PropertyBag["users"] = _userLogic.FindAll();
        }

        #endregion

        #region Users

        public void UserInfo(string userName)
        {
            SetUser();
            SetPage(PageTypes.Profile);
            SetLayout(true);

            PropertyBag["UserInfo"] = _userLogic.GetByName(userName);
        }

        public void Index()
        {
            SetPage(PageTypes.Profile);
            SetLayout(true);
            PropertyBag["AllowEdit"] = true;

            IUserActivity userActivityLogic = new UserActivityLogic();
            var userActivity = new ActivityBuilder(User, userActivityLogic);
            PropertyBag["UserActivity"] = userActivity.GetUserActivity();

            PropertyBag["CurrentTab"] = "Index";
        }

        public void Requests()
        {
            SetPage(PageTypes.Profile);
            SetLayout(true);
            PropertyBag["AllowEdit"] = true;
            PropertyBag["CurrentTab"] = "Requests";

            IFriendMovieRequest friendMovieRequestLogic = new FriendMovieRequestLogic();
            PropertyBag["Requests"] = friendMovieRequestLogic.GetPendingMovieRequestsForUser(User.Id);
        }

        #endregion

        #region Services

        public void UserNames(string profile_UserName)
        {
            SetUser();
            bool found = false;
            if (!string.IsNullOrEmpty(profile_UserName))
            {
                foreach (var c in GetUserNamesFromCache())
                {
                    if (profile_UserName.ToLower() == c.ToLower())
                    {
                        found = true;
                    }
                }
            }
            if (found)
                RenderText("false");
            else
                RenderText("true");
        }

        public void EmailAddresses(string profile_EmailAddress)
        {
            SetUser();
            bool found = false;
            if (!string.IsNullOrEmpty(profile_EmailAddress))
            {
                foreach (var c in GetEmailAddressesFromCache())
                {
                    if (profile_EmailAddress.ToLower() == c.ToLower())
                    {
                        found = true;
                    }
                }
            }
            if (found)
                RenderText("false");
            else
                RenderText("true");
        }

        public void SharingUsers()
        {
            SetUser();
            SetPage(PageTypes.Freinds);
            SetLayout(true);

            IFriends friendsLogic = new FriendsLogic();
            IList<Friends> friends = friendsLogic.GetFriendsForUser(User.Id);
            IList<UserInfo> users = new List<UserInfo>();

            foreach (var user in _userLogic.GetSharingUsers())
            {
                bool isFriend = false;
                foreach (var friend in friends)
                {
                    if (friend.Friend.Id == user.Id)
                        isFriend = true;
                }
                if (!isFriend && user.Id != User.Id)
                    users.Add(user);
            }

            PropertyBag["Users"] = users;
        }

        private IList<String> GetUserNamesFromCache()
        {
            IList<String> userNames = new List<string>();

            if (HttpContext.Current.Cache["UserNames"] != null)
                userNames = (IList<String>)HttpContext.Current.Cache["UserNames"];
            else
            {
                IList<UserInfo> users = _userLogic.FindAll();
                foreach (var user in users)
                {
                    userNames.Add(user.UserName);
                }

                HttpContext.Current.Cache.Add(
                    "UserNames", userNames,
                    null, Cache.NoAbsoluteExpiration,
                    new TimeSpan(0, 0, 900),
                    CacheItemPriority.Normal, null);
            }


            return userNames;
        }

        private IList<String> GetEmailAddressesFromCache()
        {
            IList<String> emailAddresses = new List<string>();

            if (HttpContext.Current.Cache["EmailAddresses"] != null)
                emailAddresses = (IList<String>)HttpContext.Current.Cache["EmailAddresses"];
            else
            {
                IList<UserInfo> emailAddress = _userLogic.FindAll();
                foreach (var email in emailAddress)
                {
                    emailAddresses.Add(email.EmailAddress);
                }

                HttpContext.Current.Cache.Add(
                    "EmailAddresses", emailAddresses,
                    null, Cache.NoAbsoluteExpiration,
                    new TimeSpan(0, 0, 900),
                    CacheItemPriority.Normal, null);
            }


            return emailAddresses;
        }

        #endregion

    }
}
